package com.bj58.wuxian.hystrix.hello;

import java.util.concurrent.TimeUnit;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;

public class HelloWorldHystrixCommand extends HystrixCommand<String>{
	
	private String name;
	private long sleepTime;
	
	public HelloWorldHystrixCommand(String name) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("helloGroup"))
				.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(2))
				.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(1000)));
		this.name = name;
		this.sleepTime=0;
	}
	public HelloWorldHystrixCommand(String name,long sleepTime) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("helloGroup"))
				.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(2))
				.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(1000)));
		this.name = name;
		this.sleepTime=sleepTime;
	}



	@Override
	protected String run() throws Exception {
		TimeUnit.MILLISECONDS.sleep(sleepTime);
		return "hello："+name;
	}

	@Override
	protected String getFallback() {
		
		return "fail";
	}

	
}
